package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BasePageData;
import com.cskaoyan.bean.Category;
import com.cskaoyan.bean.CategoryExample;
import com.cskaoyan.bean.admin.vo.CategoryInfoVO;
import com.cskaoyan.bean.admin.vo.CategoryL1VO;
import com.cskaoyan.bean.wx.vo.CategoryCurrentVO;
import com.cskaoyan.bean.wx.vo.CategoryIndexVO;
import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.service.CategoryService;
import com.cskaoyan.utils.Constant;
import com.cskaoyan.utils.ErrState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Jcy
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    /**
     * 商场管理
     * 商品类目初始化
     *
     * @return
     */
    @Override
    public BasePageData list() {
        List<CategoryInfoVO> categoryInfoVOS = new ArrayList<>();
        //先查找Lv1类目
        List<Category> categoriesLv1 = getCategoriesLv1();
        //遍历所有的Lv1类目，查找对应的Lv2类目，封装
        Iterator<Category> iterator = categoriesLv1.iterator();
        while (iterator.hasNext()) {
            //查找该Lv1对应的Lv2类目
            Category category = iterator.next();
            Integer pid = category.getId();
            CategoryExample exampleLv2 = new CategoryExample();
            CategoryExample.Criteria criteriaLv2 = exampleLv2.createCriteria();
            criteriaLv2.andDeletedEqualTo(false).andLevelEqualTo("L2").andPidEqualTo(pid);
            List<Category> categoriesLv2 = categoryMapper.selectByExample(exampleLv2);
            //将该Lv1类目和该类目下的Lv2类目封装到一个CategoryInfoVO对象
            CategoryInfoVO categoryInfoVO = new CategoryInfoVO(category.getId(), category.getName(), category.getKeywords(), category.getDesc(), category.getIconUrl(), category.getPicUrl(), category.getLevel(), categoriesLv2);
            //封装好的对象加入List
            categoryInfoVOS.add(categoryInfoVO);
        }
        return BasePageData.ok(categoryInfoVOS);
    }

    /**
     * 增加选项中的一级类目
     *
     * @return
     */
    @Override
    public BasePageData l1() {
        ArrayList<CategoryL1VO> categoryL1VOS = new ArrayList<>();
        List<Category> categoriesLv1 = getCategoriesLv1();
        for (Category category : categoriesLv1) {
            CategoryL1VO categoryL1VO = new CategoryL1VO();
            categoryL1VO.setValue(category.getId());
            categoryL1VO.setLabel(category.getName());
            categoryL1VOS.add(categoryL1VO);
        }
        return BasePageData.ok(categoryL1VOS);
    }

    /**
     * 添加
     *
     * @param category
     * @return
     */
    @Override
    public int create(Category category) {
        if ("L2".equals(category.getLevel()) && category.getPid() == 0) {
            return ErrState.LEVEL_ERROR.getErrno();
        }
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        int affectRows = categoryMapper.insertSelective(category);
        if (affectRows == 1) {
            return Constant.SUCCESS;
        }
        return Constant.FAIL;
    }

    /**
     * 删除
     *
     * @param categoryInfoVO
     * @return
     */
    @Override
    public void delete(CategoryInfoVO categoryInfoVO) {
        //删除一级类目
        categoryMapper.deleteByPrimaryKey(categoryInfoVO.getId());
        //将一级目录下的子目录也删除
        if (categoryInfoVO.getChildren() != null) {
            CategoryExample example = new CategoryExample();
            CategoryExample.Criteria criteria = example.createCriteria();
            criteria.andPidEqualTo(categoryInfoVO.getId());
            categoryMapper.deleteByExample(example);
        }
    }

    /**
     * 修改
     *
     * @param categoryInfoVO
     * @return
     */
    @Override
    public int update(CategoryInfoVO categoryInfoVO) {
        categoryInfoVO.setUpdateTime(new Date());
        int affectRows = categoryMapper.updateByPrimaryKeySelective(categoryInfoVO);
        if (affectRows == 0) {
            return Constant.FAIL;
        }
        return Constant.SUCCESS;
    }

    /**
     * wx
     * 类目初始化
     *
     * @return
     */
    @Override
    public CategoryIndexVO index() {
        CategoryIndexVO categoryIndexVO = new CategoryIndexVO();
        List<Category> categoryList = getCategoriesLv1();
        categoryIndexVO.setCategoryList(categoryList);
        Category category = categoryList.get(0);
        categoryIndexVO.setCurrentCategory(category);
        categoryIndexVO.setCurrentSubCategory(getCategoriesLv2(category.getId()));
        return categoryIndexVO;
    }

    /**
     * 获取指定一级类目及子类目
     *
     * @param id
     * @return
     */
    @Override
    public CategoryCurrentVO current(Integer id) {
        CategoryCurrentVO categoryCurrentVO = new CategoryCurrentVO();
        Category category = categoryMapper.selectByPrimaryKey(id);
        categoryCurrentVO.setCurrentCategory(category);
        categoryCurrentVO.setCurrentSubCategory(getCategoriesLv2(id));
        return categoryCurrentVO;
    }

    /**
     * 用来获取二级类目
     *
     * @param pid
     * @return
     */
    private List<Category> getCategoriesLv2(Integer pid) {
        CategoryExample exampleLv2 = new CategoryExample();
        CategoryExample.Criteria criteriaLv2 = exampleLv2.createCriteria();
        criteriaLv2.andDeletedEqualTo(false).andPidEqualTo(pid).andLevelEqualTo("L2");
        return categoryMapper.selectByExample(exampleLv2);
    }

    /**
     * 用来获取一级类目
     *
     * @return
     */
    private List<Category> getCategoriesLv1() {
        CategoryExample exampleLv1 = new CategoryExample();
        CategoryExample.Criteria criteriaLv1 = exampleLv1.createCriteria();
        criteriaLv1.andDeletedEqualTo(false).andLevelEqualTo("L1");
        return categoryMapper.selectByExample(exampleLv1);
    }


}
